package com.matt.components.aop;

import com.matt.commons.dto.sso.StaffDTO;
import com.matt.domain.ContractHistory;
import com.matt.foundation.annotation.OptsTrace;
import com.matt.foundation.utils.ModelUtil;
import com.matt.service.ContractOperationLogService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * **********************
 * description:合同操作日志记录
 * Created by Ron
 * from  com.matt
 * on 2018/7/24 0024.
 * **********************
 */
@Component
@Aspect
@Slf4j
public class ContractOperationLogAop {

    @Autowired
    private ContractOperationLogService contractOperationLogService;

    @Pointcut("execution(* com.matt.service.impl.*ServiceImpl.*(..)) ")
    public void executeService() {

    }

    @Around("executeService()  && @annotation(optsTrace)")
    public Object aroundOperate(ProceedingJoinPoint joinPoint, OptsTrace optsTrace) throws Throwable {
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        HttpServletRequest request = sra.getRequest();
        Object[] args = joinPoint.getArgs();

        matchAdd(request, optsTrace.code(), optsTrace.params(), args);
        return joinPoint.proceed();
    }

    /**
     * 不同类型匹配
     * @param request
     * @param code
     * @param params
     * @param args
     * @return
     */
    public ContractHistory matchAdd(HttpServletRequest request, String code, String[] params, Object[] args){
        switch (code){
            //转移给他人
            case "30001":
                return contractOperationLogService.changeStaffInfo((StaffDTO) args[0], (StaffDTO) args[1], ModelUtil.getSafeStr(args[2]));
            //中止合同
            case "30002":
                return contractOperationLogService.breakDownContract((StaffDTO)args[0],ModelUtil.getSafeStr(args[1]));
            default:
                return null;
        }
    }
}
